﻿using Bit.Art.Core.Extensions;
using Bit.Art.Core.Models;
using Bit.Art.Db;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Configuration;
using System.IO;

namespace Bit.Art.Tools
{
    class Program
    {
        internal static IConfigurationRoot Configuration { get; set; }

        private static void InitializeConfiguration()
        {
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
            var builder = new ConfigurationBuilder().SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .AddJsonFile($"appsettings.{environment}.json");
            Configuration = builder.Build();
            Utility.Configuration = Configuration;
        }

        static void Main(string[] args)
        {
            InitializeConfiguration();

            // 参考文档：https://andrewlock.net/using-dependency-injection-in-a-net-core-console-application/
            var services = new ServiceCollection();
            services.AddDbContext<ExchangeDb>(options =>
                options.UseMySql(Configuration.GetConnectionString("ExchangeDb")));
            services.AddDbContext<ExchangeConfigDb>(options =>
                options.UseMySql(Configuration.GetConnectionString("ExchangeConfigDb")));

            var serviceProvider = services
                .AddLogging()
                .BuildServiceProvider();

            using (var db = serviceProvider.GetService<ExchangeDb>())
            {
                db.Database.Migrate();
                DbInitializer.Intialize(db);
            }

            using (var db = serviceProvider.GetService<ExchangeConfigDb>())
            {
                db.Database.Migrate();
                db.Database.EnsureCreated();

                db.Markets.Add(new Core.Models.Market()
                {
                    Name = "MFS/USDT",
                    DepthInMemory = 500,
                    OrderCapacityInMemory = 100000,
                    IsEnabled = true
                });
                db.Markets.Add(new Core.Models.Market()
                {
                    Name = "MFS/BTC",
                    DepthInMemory = 500,
                    OrderCapacityInMemory = 100000,
                    IsEnabled = true
                });
                db.Markets.Add(new Core.Models.Market()
                {
                    Name = "SYC/MFS",
                    DepthInMemory = 500,
                    OrderCapacityInMemory = 100000,
                    IsEnabled = true
                });
                db.Markets.Add(new Core.Models.Market()
                {
                    Name = "BTC/USDT",
                    DepthInMemory = 500,
                    OrderCapacityInMemory = 100000,
                    IsEnabled = true
                });
                db.Markets.Add(new Core.Models.Market()
                {
                    Name = "ETH/USDT",
                    DepthInMemory = 500,
                    OrderCapacityInMemory = 100000,
                    IsEnabled = true
                });
                db.SaveChanges();
            }
        }
    }
}
